package com.itheima.controller;

import com.itheima.common.Result1;
import com.itheima.common.Result1_1;
import com.itheima.entity.User;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/frame")
public class FrameController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping("password")
    public Result1 UpdatePassword(@RequestBody Map<String, String> map) {
        return userService.UpdatePassword(map);
    }

    @PostMapping("register")
    public Result1 register(@RequestBody User user){
        return userService.register(user);
    }

    @PostMapping("logout")
    public Result1 logout(){
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        return new Result1();
    }
    @PostMapping("login")
    public Result1 login(String username, String password) {
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        return userService.login(username, password);
    }

    @PostMapping("profile")
    public Result1_1 profile() {
        return userService.profile();
    }

}
